package form_validate

import (
	"errors"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"net/http"
	"vpn/app/utils/response"
)

// openvpn的配置
type OpenVpnConfForm struct {
	OpenVpnSubNet    string `json:"open_vpn_sub_net" binding:"required"`   //子网
	OpenVpnMask      string `json:"open_vpn_mask" binding:"required"`      //子网掩码
	ClientConnEnable bool   `json:"client_conn_enable" binding:"required"` // 端对端连接
}

func (conf *OpenVpnConfForm) ValidateVpnConfig(c *gin.Context) {
	if err := c.ShouldBindJSON(conf); err != nil {
		var ve validator.ValidationErrors
		if errors.As(err, &ve) {
			for _, e := range ve {
				response.Fail(c, http.StatusBadRequest, "参数验证失败！", gin.H{"field": e.Field(), "error": e.Error()})
				return
			}
		}
		response.Fail(c, http.StatusBadRequest, "参数验证失败！", gin.H{"error": err.Error()})
		return
	}
}

type OpenVpnInstallConfForm struct {
	OpenVpnSubNet    string `json:"open_vpn_sub_net" binding:"required"`   //子网
	OpenVpnMask      string `json:"open_vpn_mask" binding:"required"`      //子网掩码
	ClientConnEnable bool   `json:"client_conn_enable" binding:"required"` // 端对端连接
	PassWord         string `json:"password" binding:"required"`           // OpenVpn证书密码
}

func (conf *OpenVpnInstallConfForm) ValidateVpnConfig(c *gin.Context) (flag bool, errorData any) {
	if err := c.ShouldBindJSON(conf); err != nil {
		var ve validator.ValidationErrors
		if errors.As(err, &ve) {
			for _, e := range ve {
				return false, gin.H{"field": e.Field(), "error": e.Error()}
			}
		}
		return false, gin.H{"error": err.Error()}
	}
	return true, nil
}
